package br.com.ordersys.web.client.modules.main;

import java.util.List;

import br.com.ordersys.web.client.modules.broker.CallBack;
import br.com.ordersys.web.client.modules.broker.ServiceBroker;
import br.com.ordersys.web.client.modules.broker.ServiceCall;
import br.com.ordersys.web.client.modules.broker.ServiceResponse;
import br.com.ordersys.web.client.modules.utils.UIUtils;
import br.com.ordersys.web.client.modules.utils.XMLUtils;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CustomScrollPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;

public class MainMenu extends SimplePanel {
	static {
		MenuResources.INSTANCE.menu().ensureInjected();
	}

	DockPanel mainPanel = null;
	VerticalPanel modulesPanel = null;
	ItemClickListener itemClickListener;

	public MainMenu() {
		super();
		setWidth("100%");
		setHeight("100%");

		ensureDebugId("mainMenu");
		
		itemClickListener = new ItemClickListener();
		buildUI();
	}

	private void buildUI() {
		mainPanel = new DockPanel();
		setWidget(mainPanel);

		mainPanel.setWidth("100%");
		mainPanel.setHeight("100%");

		modulesPanel = new VerticalPanel();
		modulesPanel.setWidth("100%");

		CustomScrollPanel scroll = new CustomScrollPanel(modulesPanel);
		scroll.setWidth("300px");
		scroll.setHeight("100%");
		scroll.setStyleName(MenuResources.INSTANCE.menu().defColors());

		scroll.getElement().setId("mainMenu_modules_scrollPane");

		UIUtils.setParentStyle(modulesPanel, "width: 100%");

		mainPanel.add(scroll, DockPanel.WEST);

		mainPanel.setCellHeight(scroll, "100%");
	}

	public void load() {
		ServiceCall sc = new ServiceCall("sys", "getSystemMenu");

		sc.setResponseCallBack(new CallBack() {
			@Override
			public void onResponse(ServiceResponse response) {
				Element body = response.getResponseBody();

				Node itens = XMLUtils.getChildNode(body, "itens");
				List<Node> nl = XMLUtils.getChildrenElements(itens, "item");

				String styleName = MenuResources.INSTANCE.menu().menuItem();

				modulesPanel.clear();

				for (int i = 0; i < nl.size(); i++) {
					Node item = nl.get(i);
					Label lbl = new Label(XMLUtils.getChildNodeText(item, "description"));
					lbl.setStyleName(styleName);
					lbl.addClickHandler(itemClickListener);

					modulesPanel.add(lbl);
				}
			}
		});

		ServiceBroker sb = new ServiceBroker();

		sb.call(sc);

	}

	private static class ItemClickListener implements ClickHandler {
		Widget currentSelected;

		@Override
		public void onClick(ClickEvent event) {
			Widget w = (Widget) event.getSource();

			if (w != currentSelected) {
				w.addStyleName(MenuResources.INSTANCE.menu().menuItemPressed());

				if (currentSelected != null) {
					currentSelected.removeStyleName(MenuResources.INSTANCE.menu().menuItemPressed());
				}
				
				currentSelected = w;
			}
		}

	}
}
